package com.tuanda.app.customviewdemo.paint;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.util.AttributeSet;

import com.tuanda.app.customviewdemo.BaseCustomView;

/**
 * Created by LiDongduo on 2018/9/21.
 */
public class PaintGetPathView extends BaseCustomView {
    private Path mPath;
    private Paint otherPaint;
    public PaintGetPathView(Context context) {
        super(context);
    }

    public PaintGetPathView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public PaintGetPathView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void initData() {
        setLayerType(LAYER_TYPE_SOFTWARE,null);
        mPath = new Path();
        mPath.moveTo(50,100);
        mPath.rLineTo(60,150);
        mPath.rLineTo(120,-180);
        mPath.rLineTo(100,120);
        mPath.rLineTo(120,-180);
        mPath.rLineTo(150,80);
        otherPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        otherPaint.setStyle(Paint.Style.STROKE);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.save();
//        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
//        mPaint.setStrokeWidth(0);

//        mPaint.setStyle(Paint.Style.STROKE);

        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(20);
        Path path1 = new Path();
        mPaint.getFillPath(mPath,path1);
        canvas.drawPath(mPath,mPaint);
        canvas.restore();

        canvas.save();
        canvas.translate(0,250);
        canvas.drawPath(path1,otherPaint);
        canvas.restore();

        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(1);
        mPaint.setTextSize(20);
        String text = "hello world";
        canvas.drawText(text,50,600,mPaint);

        Path textPath = new Path();
        otherPaint.setTextSize(20);
        otherPaint.getTextPath(text,0,text.length(),50,680,textPath);

        canvas.drawPath(textPath,otherPaint);
    }
}
